﻿Imports Microsoft.Win32

Public Class Principal
    Dim dsCaser As caserDataSet
    Private login As caserDataSet.loginRow
    Public Property Estado()
        Get
            Return lblEstado.Text
        End Get
        Set(ByVal value)
            lblEstado.Text = value
        End Set
    End Property
    Public Property rowlogin()
        Get
            Return login
        End Get
        Set(ByVal value)
            login = value
        End Set
    End Property
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim KeyPath As String = "Software\Winloc"
        Dim hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
        Dim key As RegistryKey = hklm.OpenSubKey(KeyPath, False) ' Abrimos para sólo lectura
        If (key Is Nothing) Then
            hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
            key = hklm.OpenSubKey(KeyPath, False)
            If (key Is Nothing) Then
                MessageBox.Show("Este software es pirata", "Granujas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.Close()
            End If
        End If


        dsCaser = New caserDataSet
        contarNotificaciones()
        Dim frmLogin As frmLogin
        frmLogin = New frmLogin
        frmLogin.Focus()
        frmLogin.ShowDialog()
        tReloj.Start()

        If (frmLogin.Tag Is Nothing Or key Is Nothing) Then
            Me.Close()
        Else
            rowlogin = frmLogin.Tag
            contarNotificaciones()
            If rowlogin.userName <> "franutrera" Then
                ContactosToolStripMenuItem.Visible = False
                CarteraToolStripMenuItem.Visible = False
            End If
        End If
    End Sub

    Public Sub contarNotificaciones()
        Dim fecha As Date = Date.Now.AddDays(15)


        Dim sError As String = ""

        sError = CargarVistaNotificaciones(dsCaser.VistaNotificaciones, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If


        sError = CargarVistaPoliza(dsCaser.VistaPolizas, fecha)

        If sError <> "" Then
            MessageBox.Show(sError, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        If rowlogin Is Nothing Then
            mnuNotificaciones.Text = "Notificaciones(" & dsCaser.VistaPolizas.Rows.Count & ")"

        Else
            mnuNotificaciones.Text = "Notificaciones(" & dsCaser.VistaNotificaciones.Select("idLogin =" & rowlogin.id).Count + dsCaser.VistaPolizas.Rows.Count & ")"

        End If
            End Sub

    Private Sub NuevoAlumnoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub NuevoClienteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNuevoCliente.Click
        If Not existeForm("NuevoCliente") Then
            Dim frmNuevoCliente As NuevoCliente
            frmNuevoCliente = New NuevoCliente
            frmNuevoCliente.MdiParent = Me
            frmNuevoCliente.Show()
        End If


    End Sub

    Private Sub mnuNuevaPoliza_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNuevaPoliza.Click
        If Not existeForm("frmNuevaPoliza") Then
            Dim frmNuevaPoliza As frmNuevaPoliza
            frmNuevaPoliza = New frmNuevaPoliza
            frmNuevaPoliza.MdiParent = Me
            frmNuevaPoliza.Show()
        End If
    End Sub

    Private Sub mnuGestiónDeClientes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGestiónDeClientes.Click

        If Not existeForm("frmGestionClientes") Then
            Dim frmCliente As frmGestionClientes
            frmCliente = New frmGestionClientes
            frmCliente.MdiParent = Me
            frmCliente.Show()
        End If
    End Sub

    Public Function existeForm(ByVal sNombre As String) As Boolean

        Dim Formulario As Form
        Dim Cargado As Boolean = False
        For Each Formulario In MdiChildren
            If Formulario.Name = sNombre Then
                Cargado = True
                Formulario.Select()

                Exit For
            End If
        Next Formulario

        Return Cargado
    End Function


    Private Sub tReloj_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tReloj.Tick
        lblHora.Text = Date.Now.ToLongTimeString
    End Sub

    Private Sub ConfiguraciónToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub NuevaNotificaciónToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevaNotificaciónToolStripMenuItem.Click
        If Not existeForm("frmNuevaNotificación") Then
            Dim frmNuevaNotificacion As frmNuevaNotificación
            frmNuevaNotificacion = New frmNuevaNotificación
            frmNuevaNotificacion.MdiParent = Me
            frmNuevaNotificacion.Show()
        End If
    End Sub

    Private Sub GestiónDeNotificacionesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GestiónDeNotificacionesToolStripMenuItem.Click
        If Not existeForm("frmGestionNotificaciones") Then
            Dim frmGestionNotificaciones As frmGestionNotificaciones
            frmGestionNotificaciones = New frmGestionNotificaciones
            frmGestionNotificaciones.MdiParent = Me
            frmGestionNotificaciones.Show()
        End If
    End Sub

    Private Sub ClientesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientesToolStripMenuItem.Click
        If Not existeForm("frmListadoCliente") Then
            Dim frmListadoCliente As frmListadoCliente
            frmListadoCliente = New frmListadoCliente
            frmListadoCliente.MdiParent = Me
            frmListadoCliente.Show()
        End If
    End Sub

    Private Sub CarteraToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CarteraToolStripMenuItem.Click
        If Not existeForm("frmCartera") Then
            Dim frmCartera As frmCartera
            frmCartera = New frmCartera
            frmCartera.MdiParent = Me
            frmCartera.Show()
        End If
    End Sub

    Private Sub CambiarClaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CambiarClaveToolStripMenuItem.Click

        Dim frmContraseña As frmCambiarClave
        frmContraseña = New frmCambiarClave
        frmContraseña.ShowDialog()

    End Sub

    Private Sub ContactosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactosToolStripMenuItem.Click
        If Not existeForm("frmContacto") Then
            Dim frmContacto As frmContactos
            frmContacto = New frmContactos
            frmContacto.MdiParent = Me
            frmContacto.Show()
        End If
    End Sub
End Class
